package com.easyidea.its.web.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.easyidea.its.util.ItsConstants;
import com.easyidea.its.util.URLCheckUtil;
import com.easyidea.its.web.dto.SeesionUserDto;





public class CheckFilter implements Filter{
	private String loginPage;
	
	private String backgroundLoginPage;
	
	public static final String SESSION_KEY_FROM_URL = "backUrl";
	
	public static final String SESSION_KEY_FROM_URL_BACKEND = "backUrlBackend";

	public void destroy() {
		// TODO Auto-generated method stub
		
	}

	public void doFilter(ServletRequest srequest, ServletResponse sresponse, FilterChain chain) throws IOException, ServletException {
		HttpServletRequest request = (HttpServletRequest) srequest;
		String url = request.getServletPath();
		String loginTarget = "";
		String backTarget ="";
//			if(url.contains("/background")){
//				loginTarget = backgroundLoginPage;
//				backTarget = SESSION_KEY_FROM_URL_BACKEND;
//				
//			}else{
				loginTarget = loginPage;
				backTarget = SESSION_KEY_FROM_URL;
//			}
		
		if (url.contains(loginTarget) || URLCheckUtil.notNeedCheckLoginURI(url)
				|| isLogin(srequest,url)) {
			chain.doFilter(srequest, sresponse);
		} else {
			StringBuffer fromUri = new StringBuffer();
			fromUri.append(url);
			if (request.getQueryString() != null
					&& request.getQueryString().trim().length() > 0) {
				fromUri.append("?" + request.getQueryString());
			}

			String fromUriParam = backTarget + "="
					+ java.net.URLEncoder.encode(fromUri.toString(), "UTF-8");


			
			if (loginTarget.contains("http://")) {
				StringBuffer resendUri = new StringBuffer();
				resendUri.append(loginTarget + "?");
				resendUri.append(fromUriParam);
				((HttpServletResponse) sresponse).sendRedirect(resendUri
						.toString());
			} else {
				String contextPath = ((HttpServletRequest) srequest)
						.getContextPath();
				
				((HttpServletRequest) srequest).getSession().setAttribute(
							backTarget, fromUri.toString());
			    
				if (URLCheckUtil.notNeedStoreUrI(url)) {
			    	((HttpServletRequest) srequest).getSession().setAttribute("notRedirect", "true");
				}

				String siteName = request.getParameter("site");

				StringBuffer resendUri = new StringBuffer();
				resendUri.append(contextPath + "/" + loginTarget);

				if (null == siteName || siteName.trim().length() == 0) {
					resendUri.append("?" + fromUriParam);
				} else {
					resendUri.append("?site=" + siteName + "&" + fromUriParam);
				}

				((HttpServletResponse) sresponse).sendRedirect(resendUri
						.toString());
			}

		}
		
	}
	private boolean isLogin(ServletRequest srequest,String url) {
		HttpSession session = ((HttpServletRequest) srequest).getSession();
		if (session == null ) {
			return false;
		} else {
			SeesionUserDto user =(SeesionUserDto)session.getAttribute(ItsConstants.SESSION_KEY_USER_INFO);
		//	Customer customer = (Customer) session.getAttribute(TldConstants.SESSION_KEY_CUSTOMER_INFO);
			if(null!=user&&url.contains("/auth/system")){
				return true;
			}
//			if(null!=customer&&!url.contains("/auth/background")){
//				return true;
//			}
			return false;
		}
	
	}


	public void init(FilterConfig filterConfig) throws ServletException {
		loginPage = filterConfig.getInitParameter("loginPage");	
	//	backgroundLoginPage = filterConfig.getInitParameter("backgroundLoginPage");		
	}
	


}
